<template>
  <div class="user">
    <!-- 头部 -->
    <div class="user_top">
      <!-- 头像 -->
      <img :src="headImg" alt="" />
      <!-- 显示用户状态 -->
      <h3 v-if="islogined">{{ username }}</h3>
      <h3 v-else>点击登录</h3>
      <van-icon :name="!islogined ? 'arrow' : 'cross'" @click="loginClick" />
    </div>
    <!-- 模态框遮罩层 -->
    <van-overlay v-show="isshow" class="wrapperbox">
      <div class="wrapper" @click.stop @click="isshow = false"></div>
      <!-- 表单 -->
      <div class="modal_content">
        <van-form @submit="onSubmit">
          <van-field
            v-model="username"
            name="用户名"
            label="用户名"
            placeholder="用户名"
            :rules="[{ required: true, message: '请填写用户名' }]"
          />
          <van-field
            v-model="password"
            type="password"
            name="密码"
            label="密码"
            placeholder="密码"
            :rules="[{ required: true, message: '请填写密码' }]"
          />
          <div style="margin: 16px">
            <van-button round block type="info" native-type="submit" style="background-color :red"
              >提交</van-button
            >
          </div>
        </van-form>
      </div>
    </van-overlay>
    <van-grid :column-num="3">
  <van-grid-item  icon="label-o" text="我的订单" />
  <van-grid-item  icon="bill-o" text="优惠卷" />
  <van-grid-item  icon="goods-collect-o" text="礼品卡" />
  <van-grid-item  icon="location-o" text="我的收藏" />
  <van-grid-item  icon="flag-o" text="我的足迹" />
  <van-grid-item  icon="contact" text="会员福利" />
  <van-grid-item  icon="aim" text="地址管理" />
  <van-grid-item  icon="warn-o" text="账号安全" />
  <van-grid-item  icon="service-o" text="联系客服" />
  <van-grid-item  icon="question-o" text="帮助中心" />
  <van-grid-item  icon="smile-comment-o" text="意见反馈" />

 
</van-grid>
  </div>
</template>
<script>
// 引入post登录页接口
import { getLoginData } from "@/utils/http";
// 引入图片头像
import headImg from "@/assets/images/touxiang.png";
import headImg1 from "@/assets/images/touxiang1.jpeg";

export default {
  name: "User",
  data() {
    return {
      islogined: false, //定义一个登录状态
      username: "", //登录用户名
      password: "", //密码
      isshow: false, //是否显示遮罩层
      headImg: headImg,
      headImg1: headImg1,
    };
  },
  methods: {
    // 点击提交按钮
    onSubmit(values) {
      console.log("submit", values);
      getLoginData({ username: values["用户名"], pwd: values["密码"] })
        .then((res) => {
          console.log(res);
          if (res.data.errno == 0) {
            // 登录成功
            // 01: 修改登录状态
            this.islogined = true;
            // 02: 将后端返回的token 字段存到本地
            localStorage.setItem("token", res.data.data.token);
            //03: 将后端返回的用户信息存到本地
            localStorage.setItem(
              "userinfo",
              JSON.stringify(res.data.data.userInfo)
            );
            // 04:隐藏模态框
            this.isshow = false;

            // 将用户信息显示在页面中
            this.username = res.data.data.userInfo.username;
            this.headImg = headImg1;
          } else {
            // 登录失败
          }
        })
        .catch((e) => {});
    },
    loginClick() {
      if (this.islogined) {
        // 当前是登录状态
        this.$dialog
          .confirm({
            title: "提示",
            message: "确认要退出吗",
          })
          .then(() => {
            // on confirm
            // 点击确认 ,表示确定要退出(注意退出不需要调用接口,而是修改状态)
            //01: 将本地存储的用户信息清除掉
            localStorage.removeItem("userinfo");
            localStorage.removeItem("token");
            localStorage.removeItem(headImg);
            this.headImg = headImg ;
            this.islogined = false;
          })
          .catch(() => {
            // on cancel
          });
      } else {
        //当未登录时,显示登录的模态框
        this.username = "";
        this.password = "";       
        this.isshow = true;
      }
    },
  },
   created() {
    // 当切换到我的页 重新触发创建组件实例的生命周期函数
    // console.log("created");
    // 根据本地的存储的用户信息,判断当前是否登录,如果本地存储中有用户信息,说明已登录,否则未登录
    const userinfo = JSON.parse(localStorage.getItem("userinfo"));
    if (userinfo) {
      // 说明已登录
      this.islogined = true;
      this.username = userinfo.username;
      this.headImg = headImg1 ;
    } else {
      //未登录
    }
  },
};
</script>
<style scoped lang='less'>
.user {
  .user_top {
    background: #333;
    color: #fff;
    padding: 20px 10px;
    height: 70px;
    display: flex;
    justify-content: space-between;
    align-items: center;
    img {
      width: 70px;
      height: 70px;
      border-radius: 50%;
      margin-right: 10px;
      display: block;
    }
    h3 {
      font-size: 16px;
      font-weight: 400;
      flex: 1;
    }
  }
  .wrapperbox {
    z-index: 10;
    .wrapper {
      display: flex;
      align-items: center;
      justify-content: center;
      height: 100%;
    }

    .block {
      width: 120px;
      height: 120px;
      background-color: #fff;
    }
    .modal_content {
      width: 90%;
      height: 200px;
      position: absolute;
      background: #fff;
      left: 0;
      top: 0;
      right: 0;
      bottom: 0;
      margin: auto;
      padding-top: 40px;
    }
  }
  van-grid{
    display: flex;
    flex-wrap: wrap;
    // justify-content: space-between;
    align-items: center;
   
  }
  /deep/.van-grid-item{
    padding: 20px 10px;
    }
}
</style>